import mmap
from mpi4py import MPI
import os,sys
import numpy as np
from  types import *
import util


#comm = MPI.COMM_WORLD
#p = comm.size
#rank = comm.rank
#
#if rank == 0 :
#	# create a file with dummy data
#	filename = 'testinput.in'
#	with open(filename, 'r') as f :
#		print f.fileno() == None
#		print f.fileno()
#		map = mmap.mmap(f.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
#		data = ("map", map)
#
##		buf = mmap.mmap(f.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
#else :
#	data = None
#resv = comm.bcast(data,0)

##buf = mmap.mmap(data, 0, mmap.MAP_SHARED, mmap.PROT_READ)

from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

#if  rank == 0 :
#	f = MPI.File.Open(comm,'testinput.in',MPI.MODE_RDONLY)
#else:
#	f = MPI.File.Open(comm,'testinput.in',MPI.MODE_RDONLY)
#ba = bytearray(f.Get_size())
#f.Close()

#map = mmap.mmap(f.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)

#if rank == 0 :
#	a = np.load("../experiments/input.npy", mmap_mode='r')
#else :
#	a = None
#resv = comm.bcast(a,0)
with util.Timer() as t :
	fp = np.memmap("../data/testinput10G.dat", mode='r')
print "finished np.mmap %.03f sec", t.interval

with util.Timer() as t :
	f = MPI.File.Open(comm,'../data/testinput10G.dat',MPI.MODE_RDONLY)
	ba = bytearray(f.Get_size())
	f.Iread(ba)
print "finished File.Open %.03f sec", t.interval


print "finished"
sys.exit()



#if rank == 0:
#	f = open('testinput.in', 'r')
##	map = mmap.mmap(f.fileno(), 1, mmap.MAP_SHARED, mmap.PROT_READ)
#	mat = np.load('experiments/mat3x3.npy', mmap_mode='r').
#   	data = {'key1' : mat}
#else:
#   data = None
#data = comm.bcast(data, root=0)
#print "resv at p, data: %s" % comm.rank


#http://stackoverflow.com/questions/4801284/efficient-file-buffering-scanning-methods-for-large-files-in-python
##!/usr/bin/python
#
#import mmap
#import os
#
#  def parse(string, size):
#    stride = 8
#    start = string.find("\n")
#    while start < size - stride:
#        print string[start:start+stride]
#        start += 1
#
#fasta = open("small.fasta", 'r')
#fasta_size = os.stat("small.fasta").st_size
#fasta_map = mmap.mmap(fasta.fileno(), 0, mmap.MAP_PRIVATE, mmap.PROT_READ)
#parse(fasta_map, fasta_size)